package com.jhws.business.visitor.controller;

import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.common.base.bo.Global;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.common.*;
import com.jhws.business.visitor.bo.TblVisitorDimForm;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.business.visitor.service.ITblVisitorDimService;
import com.jhws.util.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/VisitorCtrl")
public class VisitorCtrl {
    @Resource
    private ITblVisitorDimService ITblVisitorDimService;
    @Resource
    private ITblUserDimService IUserService;
    @Resource
    private OSSClientService ossClientService;

    @SuppressWarnings("unchecked")

    @RequestMapping("queryTblVisitor.x")
    public String queryTblVisitorDim(TblVisitorDimForm form, Integer page, HttpServletRequest request)throws Exception {
        StringBuilder pathParameters = new StringBuilder();
        StringBuilder pathLocalSrv = new StringBuilder();
        int currPage = page>0?page:1;
        TblUserDimBo userBo = (TblUserDimBo) request.getSession().getAttribute(TblUserDimBo.USER_INFO_REFERENCE);
        List<TblCommDimBo> commList = IUserService.getUserComms(userBo);
        if(commList.size() != 0) {
            if(!SecurityUtils.getSubject().hasRole(Global.ROLE_PROPERTY_ADMIN))
                request.setAttribute("commDimList", commList);
            else
                request.setAttribute("commDimBo", commList.get(0));

        }

		/* 判断搜索条件上来的小区号 */
        if(!StringUtils.isNotEmpty(form.getCommCode())){
            form.setCommCode(commList.get(0).getCommCode());
            form.setCommName(commList.get(0).getCommName());
        }
        pathParameters.append(form.getCommCode());

        pathParameters.append("/").append("outdoor/");

		/* 获取jsp传上来的日期 */
        String strBegin = MyRequest.GetString(request, "strBegin");
        if (!strBegin.equals("")) {
            pathLocalSrv.append(pathParameters.toString());
            pathParameters.append(strBegin).append("/").append(form.getCommCode());
            if (StringUtils.isNotEmpty(form.getAreaCode())) {
                pathParameters.append(form.getAreaCode());
            }
            if (StringUtils.isNotEmpty(form.getBuildingCode())) {
                pathParameters.append(form.getBuildingCode());
            }
        }
        request.setAttribute("strBegin", strBegin);

		/* 获取文件列表 */
        int pageSize = form.getPageSize();
        int beginIndex = pageSize * (currPage - 1);
        List<TblVisitorDimForm> tblVisitorDimForms = null;
		/* 假如是公网Web服务端，则使用拼接好的prefix去OSS中获取列表 */
        if(PropertiesReader.getProperty("isLanMode").equals("false")){

            if (currPage == 1) {
                tblVisitorDimForms = ossClientService.getVisitor(pathParameters.toString());
                request.getSession().setAttribute("tblVisitorDimForms", tblVisitorDimForms);
                Collections.reverse(tblVisitorDimForms);
            } else if (currPage > 1) {
                tblVisitorDimForms = (List<TblVisitorDimForm>) request.getSession().getAttribute("tblVisitorDimForms");
            }
        }
		/* 假如是局域网服务端，需要从本地目录获取文件列表 */
        else {

            List<String> paths = ITblVisitorDimService.getLocalVisitorsUrls(request.getSession().getServletContext().getRealPath("/")+"visitors/"+pathLocalSrv,
                    request.getSession().getServletContext().getRealPath("/")+"visitors/"+pathParameters.toString());
            tblVisitorDimForms = new ArrayList<>();
            if (currPage == 1) {
                for( String str : paths){
                    tblVisitorDimForms.add(ossClientService.getVisitorDim(str, request.getContextPath()+str));
                }
                request.getSession().setAttribute("tblVisitorDimForms", tblVisitorDimForms);
            }else if (currPage > 1) {
                tblVisitorDimForms = (List<TblVisitorDimForm>) request.getSession().getAttribute("tblVisitorDimForms");
            }
        }

        Page TblVisitorDims = new Page(beginIndex, pageSize,tblVisitorDimForms!=null?
                (tblVisitorDimForms.subList(beginIndex, (beginIndex + pageSize) < tblVisitorDimForms.size()
                        ? beginIndex + pageSize : tblVisitorDimForms.size())):null);

        TblVisitorDims.setTotalNumber(tblVisitorDimForms!=null?tblVisitorDimForms.size():0);

        PageRoll pr = new PageRoll(request, pageSize, currPage, TblVisitorDims.getTotalNumber());
        request.setAttribute("pr", pr);
        request.setAttribute("tblVisitorDims", TblVisitorDims);
        request.setAttribute("TblVisitorDimForm", form);
        return "business/visitorDim/queryTblVisitorDim";
    }
    
    /**
     *  查询访客图片
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping("queryVisitorList.x")
    @ResponseBody
    public JsonRespData queryVisitorList(@RequestParam Map param) throws Exception {
        Map<String, Object> visitorList = ITblVisitorDimService.findVisitorList(param);
        return new JsonRespData("", 1, visitorList);
    }
}
